package Dao.Imp;

import Dao.RegisterDao;
import modal.User;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.UUID;

/**
 * Created by tan on 2017/8/22.
 */
@Repository
@Transactional
public class RegisterDaoImp implements RegisterDao {

    @Autowired
    SessionFactory sessionFactory;

    public String RegisterUser(User user) {
        Session session = sessionFactory.getCurrentSession();
        try
        {
            String id = UUID.randomUUID().toString().replace("-","");
            String username=user.getUsername();
            String password=user.getPassword();
            StringBuilder Sql = new StringBuilder();
            Sql.append("INSERT INTO person_info (ID,USERNAME,PASSWORD)\n" +
                    " VALUES (?,?,?);");
            Query query = session.createSQLQuery(Sql.toString());
            query.setString(0,id);
            query.setString(1,username);
            query.setString(2,password);
            if(query.executeUpdate()==1)
            {
                return "success";
            }
        }catch (Exception e)
        {
            System.err.println("注册用户失败error");
        }
        return "error";
    }
}
